package com.GB26875_3_2011.message.parser;

import com.GB26875_3_2011.annotation.Parser;
import com.GB26875_3_2011.common.GB26875Constant;
import com.GB26875_3_2011.model.GB26875MessageBody;
import com.GB26875_3_2011.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

import static com.GB26875_3_2011.codec.MessageDecoder.parseHexTime;

/**
 * @author 蒋仕国
 * @projectName GB26875_3_211
 * @ClassName MessageBodyParser_21
 * @date 2025-01-14 10:17
 * @Description: TODO []
 */
@Parser(unitId = 21)
public class MessageBodyParser_21 implements MessageBodyParser{

    @Override
    public GB26875MessageBody parseBody(ByteBuf buffer) {
        MessageBody_21 messageBody_21 = new MessageBody_21();
        messageBody_21.status(ByteUtils.byteToBitArray(buffer.readByte()));
        String timeFlagHex = ByteUtils.readSomeByteToHex(6, buffer);
        LocalDateTime localDateTime = parseHexTime(timeFlagHex, GB26875Constant.BASE_YEARS);
        messageBody_21.setTimeFlag(localDateTime);
        return messageBody_21;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    public static class MessageBody_21 implements GB26875MessageBody {

        // 1. 正常监视状态，0.测试状态
        private int test_00;

        // 火警 （1有，0无）
        private int fire_01;

        // 故障 （1有，0无）
        private int fault_02;

        //1.主电故障 0 主电正常
        private int main_power_03;

        // 1. 备电故障 0 备电正常
        private int backup_power_04;

        // 通信故障 【1 有， 0无】
        private int comm_fault_05;
        // 1.监测链接线路故障 0 链接正常
        private int monitor_line_fault_06;
        // 7. 预留
        private int reserved_07;

        private LocalDateTime timeFlag;

        public void status(int[] status) {
            if (status.length != 8) {
                throw new RuntimeException("状态数组长度必须为8");
            }
            this.test_00 = status[0];
            this.fire_01 = status[1];
            this.fault_02 = status[2];
            this.main_power_03 = status[3];
            this.backup_power_04 = status[4];
            this.comm_fault_05 = status[5];
            this.monitor_line_fault_06 = status[6];
            this.reserved_07 = status[7];
        }
    }
}
